// weatherMap
var weatherMap = {
    "暴雪": "daxue.png",
    "暴雨": "baoyu.png",
    "暴雨到大暴雨": "baoyu.png",
    "大暴雨": "baoyu.png",
    "大暴雨到特大暴雨": "baoyu.png",
    "大到暴雪": "daxue.png",
    "大到暴雨": "dayu.png",
    "大雪": "daxue.png",
    "大雨": "dayu.png",
    "冻雨": "dongyu.png",
    "多云": "cloud.png",
    "浮尘": "yangsha.png",
    "雷阵雨": "leizhenyu.png",
    "雷阵雨伴有冰雹": "bingbao.png",
    "霾": "mai.png",
    "强沙尘暴": "shachenbao.png",
    "晴": "sun.png",
    "沙尘暴": "shachebao.png",
    "特大暴雨": "baoyu.png",
    "无": "sun.png",
    "雾": "wu.png",
    "小到中雪": "xiaoxue.png",
    "小到中雨": "xiaoyu.png",
    "小雪": "xiaoxue.png",
    "小雨": "xiaoyu.png",
    "扬沙": "yangsha.png",
    "阴": "cloudy.png",
    "雨夹雪": "dongyu.png",
    "阵雪": "daxue.png",
    "阵雨": "dayu.png",
    "中到大雪": "zhongxue.png",
    "中到大雨": "dayu.png",
    "中雪": "zhongxue.png",
    "中雨": "xiaoyu.png"
};


var timeFactory = require("~/user-module/timeFactory.js").timeFactory;
timeFactory = new timeFactory();

var geolocation = require("nativescript-geolocation");
var weatherPage;
var weatherInfo = new observable({ info: {} });
var panelView;

var jsonList;


var searchBarModule = require("ui/search-bar");

var listArray = new observableArray();

var weatherFactory = require("~/user-module/weather.js").weatherFactory;
weatherFactory = new weatherFactory();

var cityName = 123;
var animation1, animation3; var animation2;
var city;
var history = new observableArray();
exports.WeatherLoaded = args => {
    weatherPage = args.object;
    panelView = weatherPage.getViewById("panel");
    animation1 = panelView.createAnimation({ opacity: 0, translate: { x: 400, y: 0 }, duration: 0 });
    animation3 = panelView.createAnimation({ opacity: 0, translate: { x: 400, y: 0 }, duration: 500 });
    animation2 = panelView.createAnimation({ opacity: 1, translate: { x: 0, y: 0 }, duration: 500 });
    animation1.play();
    // console.log(panelView)
    // binding searchBar on submitEvent  
    searchBar = weatherPage.getViewById("search");
    searchBar.android.clearFocus();


    // latitude,longitude
    co(function* () {
        jsonList = yield readJson("json/citylist");
        // console.log(JSON.stringify(jsonList));
        city = yield readJson("json/localweather");
        city.history.forEach((item, index) => {
            history.setItem(index, item);
        });
        // console.log(JSON.stringify(city));
        if (city.cityname) {
            weatherInfo.set("info", yield weatherFactory.getweatherbycityname(city.cityname));
        } else if (city.lat) {
            weatherInfo.set("info", yield weatherFactory.getweatherbylocation(city.lat, city.lng));
        } else {
            weatherInfo.set("info", yield weatherFactory.getweatherbyip);
            city.cityname = weatherInfo.info.cityInfo.c3 || weatherInfo.info.cityInfo.c5;
            yield writeFile("json/localweather", city);
        };
        // console.log(JSON.stringify(weatherInfo.info))

        var weatherDate = {
            f1: timeFactory.getDate("YYYY-MM-DD", weatherInfo.info.f1.day),
            f2: timeFactory.getDate("YYYY-MM-DD", weatherInfo.info.f2.day),
            f3: timeFactory.getDate("YYYY-MM-DD", weatherInfo.info.f3.day),
            f4: timeFactory.getDate("YYYY-MM-DD", weatherInfo.info.f4.day),
            f5: timeFactory.getDate("YYYY-MM-DD", weatherInfo.info.f5.day),
            f6: timeFactory.getDate("YYYY-MM-DD", weatherInfo.info.f6.day),
            f7: timeFactory.getDate("YYYY-MM-DD", weatherInfo.info.f7.day)
        };
        weatherPage.bindingContext = new observable({
            weatherInfo: weatherInfo,
            deviceWidth: (DeviceInfo.widthDIPs - 20),
            listArray: listArray,
            icon: iconPackage,
            history: history,
            weatherDate: new observable(weatherDate),
            bg: "~/views/imageSource/weather/" + weatherMap[weatherInfo.info.now.weather]
        });
    }).catch(e => {
        console.log("error", e);
    });

};

exports.serachBarSubmit = args => {
    var arr = jsonList.filter((item, index) => {
        return item.label.indexOf(args.object.text) > -1;
    });
    listArray.length = 0;
    arr.forEach((item, index) => {
        listArray.setItem(index, item);
    })
    // console.log(listArray.length)
    args.object.dismissSoftInput();
    // console.log(JSON.stringify(arr[0]));

}

var _index;
exports.weatherTap = args => {
    // args.view.animate({
    //     scale: { x: 1, y: args.view.isanimated ? 1 : 1.5 },
    //     duration: 2000
    // });
};

exports.getCityname = args => {
    // console.log(args.object.text);
    co(function* () {
        weatherInfo.set("info", yield weatherFactory.getweatherbycityname(args.object.text));
        // console.log(JSON.stringify(weatherInfo));
        animation3.play();
        // console.log(weatherInfo.info.cityInfo.c3 || weatherInfo.info.cityInfo.c5)
        // console.log(JSON.stringify(city))
        // if (JSON.stringify(city).indexOf(weatherInfo.info.cityInfo.c3) < 0) {
        if (city.history.length >= 5) {
            city.history[4] = { cityname: weatherInfo.info.cityInfo.c3 };
            history.shift();
            history.push({ cityname: weatherInfo.info.cityInfo.c3 });
        } else {
            city.history.push({ cityname: weatherInfo.info.cityInfo.c3 });
            history.push({ cityname: weatherInfo.info.cityInfo.c3 });
        };
        // };
        city.cityname = weatherInfo.info.cityInfo.c3 || weatherInfo.info.cityInfo.c5;
        // console.log(JSON.stringify(city))
        yield writeFile("json/localweather", city);
    });
};

var _index;
exports.changeCity = args => {
    try { animation2.play(); } catch (e) {
        console.log(e);
    };
    searchBar.focus();
};

exports.back = args => {
    try {

        animation3.play();
    } catch (e) {
        console.log(e);
    }
}